import pandas as pd
from dao.DeptDao import DeptDao
from Static import Static
from StatusCode import StatusCode


class DeptService:
    def initDB(self):
        dao = DeptDao()
        df1 = self.getDeptList()
        if df1.shape[0] == 0:
            # 初始化部门信息
            depts = [
                {
                    "name": "信息科",
                    "state": "1",
                },
                {
                    "name": "儿科门诊",
                    "state": "1",
                },
                {
                    "name": "妇科门诊",
                    "state": "1",
                },
                {
                    "name": "产科门诊",
                    "state": "1",
                },
                {
                    "name": "内科门诊",
                    "state": "1",
                },
                {
                    "name": "外科门诊",
                    "state": "1",
                },
                {
                    "name": "中医康复科",
                    "state": "1",
                },
                {
                    "name": "儿一科",
                    "state": "1",
                },
                {
                    "name": "儿二科",
                    "state": "1",
                },
                {
                    "name": "妇科病区",
                    "state": "1",
                },
                {
                    "name": "产前区",
                    "state": "1",
                },
                {
                    "name": "爱婴区",
                    "state": "1",
                },
            ]
            df = pd.DataFrame(data=depts, columns=depts[0].keys())
            dao.addDepts(df)

    # 查询部门列表
    def getDeptList(self):
        df = DeptDao().queryDepts()
        return df

    # 获取特定部门信息
    def getDeptInfo(self, json_data):
        deptId = json_data.get("deptId", -1)
        df = DeptDao().getDeptInfo(deptId)
        return df

    # 更新部门信息
    def updateDept(self, json_data):
        try:
            deptId = json_data.get("id", -1)
            if deptId == -1:
                return StatusCode.RESPONSE_FAIL
            deptName = json_data.get("name", "").strip()
            dao = DeptDao()
            dao.updateDept(deptId, deptName)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    # 新增部门
    def addDept(self, json_data):
        try:
            deptName = json_data.get("name", "").strip()
            dao = DeptDao()
            df = dao.getDeptInfoByName(deptName)
            if df.shape[0] > 0:
                return StatusCode.RESPONSE_EXIST
            state = json_data.get("state", "1")
            dept = {"name": deptName, "state": state}
            df = pd.DataFrame(data=[dept], columns=dept.keys())
            dao.addDepts(df)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    # 删除部门
    def delDept(self, json_data):
        try:
            deptId = json_data.get("deptId", -1)
            if deptId == -1:
                return StatusCode.RESPONSE_FAIL
            DeptDao().delDept(deptId)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL

    # 部门状态切换
    def changeDeptState(self, json_data):
        try:
            deptId = json_data.get("id", -1)
            state = json_data.get("state", "1").strip()
            if deptId == -1:
                return StatusCode.RESPONSE_FAIL
            dao = DeptDao()
            dao.changeDeptState(deptId, state)
            return StatusCode.RESPONSE_SUCCESS
        except Exception as e:
            print(e)
            return StatusCode.RESPONSE_FAIL
